package com.seeyon.apps.lzmzj.kit;

import sun.misc.BASE64Encoder;

import javax.servlet.http.HttpServletRequest;

public class AuthenticationKit {
    public static String isAuthenticated(HttpServletRequest request, String userName, String password, String key) {
        String authorization = StrKit.str(request.getHeader("Authorization"));
        if ("".equals(authorization)) {
            String result = MessageKit.msg("E", key, "", "认证信息不能为空,认证方式为 Basic Auth");
            return result;
        }
        String account = userName + ":" + password;
        String authorization_new = "Basic " + new BASE64Encoder().encode(account.getBytes());
        if (!authorization.equals(authorization_new)) {
            String result = MessageKit.msg("E", key, "", "认证失败,账号或密码错误!");
            return result;
        }
        return "";
    }
}
